package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    private R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        log.info("登录，employee = {}",employee);
        //mb5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        //失败
        if (one==null){
            return R.error("用户名或密码错误");
        }
        //密码比对
        if(!password.equals(one.getPassword())){
            return R.error("用户名或密码错误");
        }
        //状态
        if (one.getStatus() == 0 ){
            return R.error("该用户被禁用");
        }
        //成功,用户信息存入session
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        log.info("退出");
        request.getSession().invalidate();
        return R.success();
    }

    //保存员工
    @PostMapping
    public R save(HttpServletRequest request,@RequestBody Employee employee){

        //补充实体属性
        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes()));
        employee.setStatus(1);

//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        //保存
        employeeService.save(employee);
        return R.success();
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(@RequestParam(required =false,defaultValue = "1") int page ,
                                  @RequestParam(required =false,defaultValue = "1") int pageSize , String name){
        //封装
         Page<Employee> pageInfo = new Page<>(page,pageSize);
        //组装查询条件，排序条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行分页查询
        Page<Employee> page1 = employeeService.page(pageInfo, queryWrapper);
        //返回结果
        return R.success(page1);
    }

    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        //补全属性
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        //更新

        employeeService.updateById(employee);
        return R.success();
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id ){

        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("查询的员工不存在");
    }
}
